package com.yequan.designpattern.observer.wechat;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者实例
 *
 * @author yequan
 * @date 2019/12/12
 */
public class WechatServer implements Observerable {

    /**
     * 观察者集合
     */
    private List<Observer> observerList;

    /**
     * 通知消息
     */
    private String message;

    public WechatServer() {
        observerList = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observerList.remove(observer);
    }

    @Override
    public void notifyObserver() {
        if (observerList.size() > 0) {
            for (Observer observer : observerList) {
                observer.update(message);
            }
        } else {
            System.err.println("没有观察者");
        }
    }

    public void setInfo(String message) {
        this.message = message;
        System.out.println("微信推送的消息为: " + message);
        notifyObserver();
    }
}
